package com.wwwarehouse.resource_center.customView;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wwwarehouse.common.tools.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph on 2017/9/15.
 *         <p/>
 *         和输入框绑定的Button,当绑定的所有输入框都有内容时，自动可点击，只要有一个输入框没内容则不可点击
 */

public class AutoClickButton extends Button {

    private List<TextView> texts = new ArrayList<>();

    public AutoClickButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEnabled(false);
    }
    public void bindEditText(TextView edt){
        texts.add(edt);
        for (int i = 0; i < texts.size(); i++) {
            texts.get(i).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    changeStatus();
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }
    public void removeAllView(){
        texts.clear();
    }
    private void changeStatus() {
        for (int i = 0; i <texts.size() ; i++) {
            String ss = texts.get(i).getText().toString().trim();
            if (TextUtils.isEmpty(ss)){
                setEnabled(false);
                return;
            }
        }
        setEnabled(true);
    }
}
